//
//  CLReadConfigure.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/16.
//

#import "CLReadConfigure.h"
#import <objc/runtime.h>
#import <YYKit/YYClassInfo.h>
static CLReadConfigure *shared = nil;
@implementation CLReadConfigure
CLEncodeMethod


- (UIColor *)bgColor  {
    if (self.bgColorIndex == [CL_READ_BG_COLORS indexOfObject:CL_COLOR_BG_0]) { // 牛皮黄背景
        return [UIColor colorWithPatternImage:[UIImage imageNamed:@"read_bg_0"]];
    }else{
        return CL_READ_BG_COLORS[self.bgColorIndex];
    }
}

/// 字体颜色
- (UIColor *)textColor {
    
    // 固定颜色
    return CL_COLOR_145_145_145;

    // 根据背景颜色选择
//        return DZM_READ_TEXT_COLORS[bgColorIndex.intValue]
    
    // 字体颜色列表选择
//        return DZM_READ_TEXT_COLORS[textColorIndex.intValue]
    
    // 日夜间切换
//         if DZMUserDefaults.bool(DZM_READ_KEY_MODE_DAY_NIGHT) {
//
//            return DZM_COLOR_145_145_145
//
//         }else{ return DZM_COLOR_145_145_145 }
}

- (UIColor *)statusTextColor {
    
    // 固定颜色
    return CL_COLOR_145_145_145;
    
    // 根据背景颜色选择
//        return DZM_READ_STATUS_TEXT_COLORS[bgColorIndex.intValue]
    
    // 字体颜色列表选择
//         return DZM_READ_STATUS_TEXT_COLORS[statusTextColorIndex.intValue]
    
    // 日夜间切换
//         if DZMUserDefaults.bool(DZM_READ_KEY_MODE_DAY_NIGHT) {
//
//            return DZM_COLOR_145_145_145
//
//         }else{ return DZM_COLOR_145_145_145 }
}
/// 行间距(请设置整数,因为需要比较是否需要重新分页,小数点没法判断相等)
- (CGFloat)lineSpacing {
    if (self.spacingType == CLSpacingTypeBig) { // 大间距
        return CL_SPACE_10;
    } else if (self.spacingType == CLSpacingTypeMiddle) { // 中间距
        return CL_SPACE_7;
    } else { // 小间距
        return CL_SPACE_5;
    }
}

/// 段间距(请设置整数,因为需要比较是否需要重新分页,小数点没法判断相等)
- (CGFloat)paragraphSpacing {
    if (self.spacingType == CLSpacingTypeBig) { // 大间距
        return CL_SPACE_20;
    } else if (self.spacingType == CLSpacingTypeMiddle) { // 中间距
        return CL_SPACE_15;
    } else { // 小间距
        return CL_SPACE_10;
    }
}

/// 阅读字体
- (UIFont *)fontByIsTitle:(BOOL)isTitle {
    CGFloat size = CL_SIZE(self.fontSize + (isTitle ? CL_READ_FONT_SIZE_SPACE_TITLE : 0));
    CLFontType fontType = self.fontType;
    switch (fontType) {
        case CLFontTypeSystem:
            return [UIFont systemFontOfSize:size];
            break;
        case CLFontTypeOne:
            return [UIFont fontWithName:@"EuphemiaUCAS-Italic" size:size];
            break;
        case CLFontTypeTwo:
            return [UIFont fontWithName:@"AmericanTypewriter-Light" size:size];
            break;
        case CLFontTypeThree:
            return [UIFont fontWithName:@"Papyrus" size:size];
            break;
    }
}

/// 字体属性
/// isPaging: 为YES的时候只需要返回跟分页相关的属性即可 (原因:包含UIColor,小数点相关的...不可返回,因为无法进行比较)
- (NSDictionary *)attributesByIsTitle:(BOOL)isTitle isPageing:(BOOL)isPageing {
    // 段落配置
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    // 当前行间距(lineSpacing)的倍数(可根据字体大小变化修改倍数)
    paragraphStyle.lineHeightMultiple = 1.0;
    if (isTitle) {
        // 行间距
        paragraphStyle.lineSpacing = 0;
        // 段间距
        paragraphStyle.paragraphSpacing = 0;
        // 对其
        paragraphStyle.alignment = NSTextAlignmentCenter;
    } else {
        // 行间距
        paragraphStyle.lineSpacing = self.lineSpacing;
        // 换行模式（避免每页尾部留空白）
        paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
        // 段间距
        paragraphStyle.paragraphSpacing = self.paragraphSpacing;
        // 对其
        paragraphStyle.alignment = NSTextAlignmentJustified;
    }
    
    if (isPageing) {
        return @{NSFontAttributeName: [self fontByIsTitle:isTitle],NSParagraphStyleAttributeName: paragraphStyle};
    } else {
        return @{NSForegroundColorAttributeName: self.textColor, NSFontAttributeName: [self fontByIsTitle:isTitle],NSParagraphStyleAttributeName: paragraphStyle};
    }
}
// MARK: 辅助

/// 保存(使用 DZMUserDefaults 存储是方便配置修改)
- (void)save {
    
    NSDictionary *dict = @{@"bgColorIndex": @(self.bgColorIndex),
                           @"fontIndex": @(self.fontIndex),
                           @"effectIndex": @(self.effectIndex),
                           @"spacingIndex": @(self.spacingIndex),
                           @"progressIndex": @(self.progressIndex),
                           @"fontSize": @(self.fontSize)};
    
    [CLUserDefaults setObject:dict forKey:CL_READ_KEY_CONFIGURE];
}

- (CLProgressType)progressType { return (CLProgressType)self.progressIndex; }

- (CLEffectType)effectType { return (CLEffectType)self.effectIndex; }

+ (CLReadConfigure *)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared= [[CLReadConfigure alloc] initWithDict:nil];
        NSDictionary *dictionary = [CLUserDefaults dictionaryForKey:CL_READ_KEY_CONFIGURE];
        if ([dictionary isKindOfClass:[NSDictionary class]]) {
            [shared setValuesForKeysWithDictionary:dictionary];
        }
    });
    return shared;
}

- (instancetype)initWithDict:(NSDictionary *)dict {
    self = [super init];
    if (self) {
        [self initData];
    }
    return self;
}


- (void)initData {
    // 背景
    if ((self.bgColorIndex <= 0) || (self.bgColorIndex >= CL_READ_BG_COLORS.count) ){
        
        self.bgColorIndex = [CL_READ_BG_COLORS indexOfObject:CL_COLOR_BG_0];
    }
    // 字体类型
    if (self.fontIndex <= 0) {
        
        self.fontIndex = CLFontTypeTwo;
    }
    // 间距类型
    if (self.spacingIndex <= 0) {
        
        self.spacingIndex = CLSpacingTypeSmall;
    }
    
    // 翻页类型
    if (self.effectIndex <= 0) {
        
        self.effectIndex = CLEffectTypeSimulation;
    }
    
    // 字体大小
    if ((self.fontSize <= 0) || (self.fontSize > CL_READ_FONT_SIZE_MAX || self.fontSize < CL_READ_FONT_SIZE_MIN) ){
        
        self.fontSize = CL_READ_FONT_SIZE_DEFAULT;
    }
    
    // 显示进度类型
    if (self.progressIndex <= 0) {
        
        self.progressIndex = CLProgressTypePage;
    }
    self.openLongPress = YES;
}

+ (CLReadConfigure *)modelBy:(NSDictionary *)dict {
    return [[CLReadConfigure alloc]initWithDict:dict];
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {}
@end
